package com.itheima.controller;


import com.itheima.common.DefaultException;
import com.itheima.common.R;
import com.itheima.dto.OrganizationDto;
import com.itheima.dto.RoleDto;
import com.itheima.service.OrganizationService;
import com.itheima.vo.organization.OrganizationBriefVo;
import com.itheima.vo.organization.OrganizationDetailVo;
import com.itheima.vo.organization.OrganizationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * (Organization)表控制层
 * 权限、组织相关的操作
 *
 * @author makejava
 * @since 2022-10-01 22:01:17
 */
@RestController
@RequestMapping("organization")
@Api(tags = "组织管理api")
public class OrganizationController {

    @Resource
    private OrganizationService organizationService;

    /**
     * 查找当前用户能向下管理的权限
     *
     * @return
     */
    @ApiOperation(value = "查找当前用户能向下管理的权限Api")
    @GetMapping("/findRole")
    public R<OrganizationVo> findRole() throws Exception {
        return R.success(organizationService.findRole());
    }

    /**
     * 更新用户权限
     *
     * @param roleDto
     * @return
     */
    @ApiOperation(value = "更新用户权限Api", notes = "前端传参eg：{\n" +
            "    \"userIds\": [\n" +
            "        \"19251104252\",\n" +
            "        \"19251104253\"\n" +
            "    ],\n" +
            "    \"organizationId\": 6\n" +
            "}")
    @PutMapping("/updateRole")
    public R<String> updateRole(@RequestBody @Validated RoleDto roleDto) throws Exception {
        organizationService.updateRole(roleDto);
        return R.success("权限更新成功");
    }

    /**
     * 新增组织
     *
     * @param organizationDto
     * @return
     */
    @ApiOperation(value = "新增组织")
    @PostMapping("/insertOrganization")
    public R<String> insertOrganization(@RequestBody @Validated OrganizationDto organizationDto) throws Exception {
        organizationService.insertOrganization(organizationDto);
        return R.success("新增成功");
    }

    /**
     * 更新组织信息（名称）
     *
     * @param organizationDto
     * @return
     */
    @ApiOperation(value = "更新组织信息（名称）")
    @PutMapping("/updateOrganization")
    public R<String> updateOrganization(@RequestBody @Validated OrganizationDto organizationDto) throws Exception {
        organizationService.updateOrganization(organizationDto);
        return R.success("更新成功");
    }

    /**
     * 删除组织
     *
     * @param organizationId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除组织")
    @DeleteMapping("/deleteOrganization")
    public R<String> deleteOrganization(@NotNull @RequestParam("organizationId") Integer organizationId) throws Exception {
        organizationService.deleteOrganization(organizationId);
        return R.success("删除成功");
    }

    /**
     * 展示组织详情
     * @param organizationId 组织id
     * @return 组织详情vo
     */
    @ApiOperation(value = "展示组织详情")
    @GetMapping("/showDetail")
    public R<OrganizationDetailVo> showDetail(@RequestParam("organizationId") Integer organizationId) throws DefaultException {
        return R.success(organizationService.showDetail(organizationId));
    }

    /**
     * 展示组织列表
     * @param keyword 关键字
     * @return 组织简略信息vo
     */
    @ApiOperation(value = "展示组织列表")
    @GetMapping("listOrganization")
    public R<List<OrganizationBriefVo>> listOrganization(@RequestParam("keyword") String keyword){
        return R.success(organizationService.listOrganization(keyword));
    }

    /**
     * 导出组织下用户信息
     * @param organizationId 组织id
     * @param response 响应对象
     */
    @ApiOperation(value = "导出组织下用户信息")
    @GetMapping("/excelExport")
    public void excelExport(@RequestParam("organizationId") Integer organizationId, HttpServletResponse response) throws IOException {
        organizationService.excelExport(organizationId,response);
    }
}

